var PW = PW || {};

PW.addNewSlot = {

    options:function (options) {
        var _this = this;
        //console.log('options:');
        //console.log(options);
    },

    load:function () {
    },

    el:{ },

    events:function () {
        var _this = this;

        $('#Venue').on('change', function () {
            var value = $(this).val();
            //console.log('venue', value);
            $('[data-venue]').hide();
            var el = $('[data-venue="' + value + '"]');
            el.show();
            $('#Pitch').val($(el[0]).val())
        });
        $('#Venue').trigger('change');

        $('#SlotForm').on('submit', function () {
            var formData = $('#SlotForm').serializeObject();
            //console.log('formData', formData);

            var obj = {};
            obj.action = 'add_slot';
            obj.form = formData;

            if (obj.form.form_day == undefined) {
                console.error('no day specified');
                return false;
            }

            if (!(obj.form.form_day instanceof Array)) {
                //console.log('obj.form.form_day', obj.form.form_day);
                obj.form.form_day = [obj.form.form_day];
            }

            PW.ajax(obj,function(response){
                $('.slot-success,.slot-denied').html('')
                for(var a in response.newSlots) {
                    var item = response.newSlots[a];
                    var z = $('<div/>',{
                        html:item.day.toString() + ' ' + item.start_time + ' - ' + item.end_time
                    });
                    $('.slot-success').append(z)
                }
                for(var a in response.deniedSlots) {
                    var item = response.deniedSlots[a];
                    var z = $('<div/>',{
                        html:item.day.toString() + ' ' + item.start_time + ' - ' + item.end_time
                    });
                    $('.slot-denied').append(z)
                }

                try {
                    PW.myCalendar.loadCalendar();
                } catch (e) { }

            });

            return false;
        });

    },

    init:function (options) {
        this.options(options);
        this.events();
    }
};

$.fn.serializeObject = function () {
    var o = {};
    var a = this.serializeArray();
    $.each(a, function () {
        if (o[this.name] !== undefined) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
};


